package com.rt.generalframework.core.aop;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class ControllerAspect {
	private JSONObject jsonObject = new JSONObject();

	//申明一个切点 里面是 execution表达式
	@Pointcut("execution(public * com.rt.generalframework.web.*.*(..))")
	private void controllerAspect() {
	}

	@Around("controllerAspect()")
	public Object methodAround(ProceedingJoinPoint joinPoint) {
		//开始时间
		long startTimeMillis = System.currentTimeMillis();
		//获取请求的方法
		String methodName = joinPoint.getSignature().getName();
		//获取请求参数
		Object[] args = joinPoint.getArgs();
		log.info("【" + methodName + "】请求内容：{}", args.length > 0 ? args[0].toString() : "null");
		Object result = null;
		try {
			//执行被切的函数，result为被切目标函数返回的内容
			result = joinPoint.proceed();
			log.info("【" + methodName + "】响应内容：{}", jsonObject.toJSONString(result));
			log.info("【" + methodName + "】响应耗时：{}毫秒", System.currentTimeMillis() - startTimeMillis);
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}
		return result;
	}


}
